/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package braipetro.interfaces.regras;

import java.util.Dictionary;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author manasses
 */
public class Regra {
    private String nome;
    private String formula = "";
    private boolean started = false;
    private boolean triggered = false;
    
    private List<Token> token = new LinkedList<>();
    
    private Dictionary<String, Criterio> criterios = new Hashtable<>();
    private Dictionary<String, Metodo> metodos = new Hashtable<>();
    
//------------------------------------------------------------------------------
// Constructors
//------------------------------------------------------------------------------
    
    public Regra() {
    
    }
    
//------------------------------------------------------------------------------
// Gets and Sets
//------------------------------------------------------------------------------
    
    public boolean getStatus() {
        return this.started;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getFormula() {
        return formula;
    }

    public void setFormula(String formula) {
        this.formula = formula;
    }
    
    public boolean isTriggered() {
        return this.triggered;
    }
    
    public boolean isStarted() {
        return this.started;
    }
    

//------------------------------------------------------------------------------
// Private Methods
//------------------------------------------------------------------------------
    
    private Boolean validarFormula() {
        return false;
    }
    
    private void toPosfixa() {
        
    }
    
    
    
//------------------------------------------------------------------------------
// Public Methods
//------------------------------------------------------------------------------
    
    public void addCriterio(Criterio crit) {
        criterios.put(crit.getNome(), crit);
        
        crit.setRegra(this);
    }
    
    public void trigger() {
         
        Enumeration<String> enu = metodos.keys();
        
        while (enu.hasMoreElements()) {
            metodos.get(enu.nextElement()).execute();
        }
        
        this.triggered = true;
    }
    
    public void addAction(Metodo mtd) {
        metodos.put(mtd.getNome(), mtd);
    }
    
    public void start() {
        Enumeration<String> enu = criterios.keys();
        
        while (enu.hasMoreElements()) {
            Criterio cri = criterios.get(enu.nextElement());
            cri.setAtivo(true);
            cri.start();
        }
    }
   
    public void stop() {
        Enumeration<String> enu = criterios.keys();
        
        while (enu.hasMoreElements()) {
            criterios.get(enu.nextElement()).setAtivo(false);
            
        }
    }
    
    public void restart() {
        stop();
        start();
    }
    
    
}
